
class HandCardPromptPanel extends eui.Panel {
    static localKey: string = "hand_cBox_select";
    private group_p: eui.Group;
    private auto_cBox: eui.CheckBox;
    private img_bg: eui.Image;
    //牌组
    public cardGroup: eui.DataGroup;
    private pais: eui.ArrayCollection;
    private scroller_pai: eui.Scroller;

    constructor() {
        super();
        this.skinName = "HandCardPromptSkin";
        this.horizontalCenter = 0;
        this.top = 10;

        this.auto_cBox.addEventListener(eui.UIEvent.CHANGE, this.cBoxChange, this);

        this.cardGroup.itemRenderer = CardViewItem;
        this.pais = new eui.ArrayCollection(new Array());
        this.cardGroup.dataProvider = this.pais;
    }

    public init(pai?: any) {
        this.visible = true;
        this.formatData(pai);
        this.autoView();
        this.updateView(true);
    }
    /**适配宽度 */
    private autoView() {
        const autoWidth: number = 184;
        const autoItemWidth: number = 92;
        const horizontalGap: number = 24;
        let num: number = this.pais.length;
        if (num > 7) { num = 7 };
        var sWidth: number = autoItemWidth * num - horizontalGap;
        this.scroller_pai.width = sWidth;
        this.width = autoWidth + sWidth;
        this.group_p.width = this.width;

    }

    private formatData(pai) {
        this.pais.removeAll();
        let hcData = pai ? GSDataProxy.i.getHandCardPromptByPai(pai) : GSData.i.handTingCard;
        let info: any[] = hcData.info;
        info.forEach((item) => {
            let data = new CardItemData();
            data.count = item.pai_count;
            data.pai = item.pai;
            data.fan = item.fan;
            this.pais.addItem(data);
        });
    }

    private cBoxChange(e: egret.Event) {
        const cBox: eui.CheckBox = e.currentTarget;
        GSData.i.autoPushPai = cBox.selected;
        gameManager.socketManager.send(41, { pos: PublicVal.i.getPos(1), flag: cBox.selected });
    }

    public clean() {
        this.visible = false;
        this.pais.source = [];
    }


    private cardConfig = {
        w: 68,
        dx: 6,
        dy: 45,
    }

    public updateView(flag?: boolean) {
        this.auto_cBox.selected = GSData.i.autoPushPai;
        this.auto_cBox.visible = flag == undefined ? !game.isHu && GSData.i.autoPushBtnVis : flag;
        if (game.isHu) {
            this.auto_cBox.visible = false;
        }
        this.pais.refresh();
    }
}

class CardViewItem extends eui.ItemRenderer {

    private img_pai: eui.Image;
    private lab_fan: eui.Label;
    private lab_count: eui.Label;
    public data: CardItemData;
    constructor() {
        super();
        this.skinName = "CardViewItemSkin";
    }

    protected dataChanged() {
        super.dataChanged();
        const data = this.data;
        this.lab_fan.text = `${data.fan}番`;
        this.lab_count.text = `${data.count}`;
        this.img_pai.source = `Z_${data.pai.type}${data.pai.number}`;
    }


}

class CardItemData {
    pai: any;
    fan: number;
    count: number;
}



